<?php
/*
* @Name:sitemap.php
* @Version:1.1
* @Use: to generate the XML sitemap file.
* @Author: CoderChao
*/

/*
// Check if sitemap exists
if (file_exists('sitemap.xml'))
{
	header('HTTP/1.1 301 Moved Permanently'); 
	header("Location: sitemap.xml");
}
else
*/
include 'function.php';
$KeywordsArray = get_keywords_array('./','sitemap');	//get the keywords array
$sitemap 	   = '<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="http://'.$_SERVER['HTTP_HOST'].'/style/sitemap.xsl"?>
<urlset
    xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
    http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">';

$sitemap 	  .='<url>
<loc>http://'.$_SERVER['HTTP_HOST'].'/</loc>
<changefreq>hourly</changefreq>
<priority>1.00</priority>
</url>';

foreach ($KeywordsArray as $keywords)
{
	if(stripos($kewords,'free') === false)
	{
	$keyword  =  clean_keyword($keywords);
	$url 	  = 'http://'.$_SERVER['HTTP_HOST'].'/'.strtolower($keyword).'.html';
	$filename = './html/'.md5(strtolower(str_replace('-', ' ', $keyword))).'.txt';
	if(file_exists($filename))
	{ $lastmod = date("c",filectime("$filename")); }
	else
	{ $lastmod = date("c"); }
	$sitemap .="\n".'<url>
	<loc>'.$url.'</loc>
	<changefreq>weekly</changefreq>
	<priority>0.80</priority>
	<lastmod>'.$lastmod.'</lastmod>
	</url>';
	}
}

$sitemap .= "\n</urlset>";
$fp 	  = file_put_contents('sitemap.xml', $sitemap);

/* Unset the array and string */
$KeywordsArray = NULL;
$sitemap 	   = NULL;

if($fp)
{
	header('HTTP/1.1 301 Moved Permanently'); 
	header("Location: sitemap.xml");
}
?>